package com.hancher.gamelife2.compass;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.hancher.common.androidutil.LogUtil;
import com.hancher.common.base.BaseMvpFragment;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.FragmentCompressBinding;
import com.hancher.gamelife2.home.HomePresenter;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 描述 : 加密 <br/>
 * 时间 : 2024/5/21 14:46 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class CompassFragment extends BaseMvpFragment<FragmentCompressBinding, HomePresenter> {
    private SensorManager manager;
    private SensorEventListener listener = new SensorEventListener() {
        private float predegree = 0;

        @Override
        public void onSensorChanged(SensorEvent event) {
            float degree = event.values[0];// 存放了方向值 90东 270西 0北 180南
            RotateAnimation animation = new RotateAnimation(predegree, -degree,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(200);
            binding.image.startAnimation(animation);
            predegree = -degree;
            String str = "指南针";
            if (degree < 1 && degree > 359) {
                str = "正北";
            } else if (degree < 91 && degree > 89) {
                str = "正东";
            } else if (degree < 181 && degree > 179) {
                str = "正南";
            } else if (degree < 271 && degree > 269) {
                str = "正西";
            } else if (degree > 0 && degree < 45) {
                str = String.format("北偏东\n%s 度", BigDecimal.valueOf(degree).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 45 && degree < 90) {
                str = String.format("东偏北\n%s 度", new BigDecimal(90).subtract(BigDecimal.valueOf(degree)).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 90 && degree < 135) {
                str = String.format("东偏南\n%s 度", BigDecimal.valueOf(degree).subtract(new BigDecimal(90)).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 135 && degree < 180) {
                str = String.format("南偏东\n%s 度", new BigDecimal(180).subtract(BigDecimal.valueOf(degree)).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 180 && degree < 225) {
                str = String.format("南偏西\n%s 度", BigDecimal.valueOf(degree).subtract(new BigDecimal(180)).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 225 && degree < 270) {
                str = String.format("西偏南\n%s 度", new BigDecimal(270).subtract(BigDecimal.valueOf(degree)).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 270 && degree < 315) {
                str = String.format("西偏北\n%s 度", BigDecimal.valueOf(degree).subtract(new BigDecimal(270)).setScale(2, RoundingMode.HALF_UP).toString());
            } else if (degree > 315 && degree < 360) {
                str = String.format("北偏西\n%s 度", new BigDecimal(360).subtract(BigDecimal.valueOf(degree)).setScale(2, RoundingMode.HALF_UP).toString());
            }
            str += "\n" + String.format("指南针(%s)", BigDecimal.valueOf(degree).setScale(6, RoundingMode.HALF_UP));
            LogUtil.d("当前角度" + degree);
            binding.txtTop.setText(str);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };


    @Override
    protected void initView(Bundle savedInstanceState) {
        binding.image.setImageResource(R.drawable.ljq);
    }

    @Override
    protected void initListener() {
    }

    @Override
    protected void initData() {
        manager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
        binding.image.setKeepScreenOn(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public void onPause() {
        manager.unregisterListener(listener);
        super.onPause();
    }
}